//这是入口文件
//使用 Vue的runtime-only模式
import Vue from 'vue'
import app from'./app.vue'

//导入mint组件
// import { Header,Swipe, SwipeItem,Button,Lazyload  } from 'mint-ui';
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

//导入mui样式
import './lib/Mui/css/mui.css'
import './lib/Mui/css/icons-extra.css'

//导入router
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = 'https://www.apiopen.top/';

//导入vue-preview
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//导入moment
import moment from 'moment'

//导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)

//获取内地存储中的商品数量
var car = JSON.parse(localStorage.getItem('car')||'[]')

//定义vuex实例
const store = new Vuex.Store({
    state: {
        count: 0,
        car:car
    },
    mutations:{
        shoppingCar(state,goodsInfo){


           var flag = false

           state.car.some(item => {
               if(item.id == goodsInfo.id){
                   item.count+=parseInt(goodsInfo.count)
                        flag = true
                        return
                   }
               })

               if(!flag){
                   state.car.push(goodsInfo)
            }
            localStorage.setItem('car',JSON.stringify(state.car))

        },
        updateNum(state,newDate){
            state.car.some(item => {
                if(item.id == newDate.id){
                    item.count = newDate.count
                }
                return
            })

            localStorage.setItem('car',JSON.stringify(state.car))
        },
        changeState(state,val){
            state.car.some(item => {
                if(item.id == val.id ){
                    item.selected = val.selected
                }
                return
            })

            localStorage.setItem('car',JSON.stringify(state.car))
        },
        deleGoods(state,id){
            //删除购物车中商品列表
            state.car.some((item,i) => {
                if(item.id == id ){
                    state.car.splice(i,1)
                }
            })

            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },

    getters:{
        getCounts(state){
            var c = 0;
            state.car.forEach(item =>{
                c+= parseInt(item.count)
                
            })
            return parseInt(c)
            
        },
        getNum(state){
            // console.log(state.car)
            var num = {};
            state.car.forEach(item => {
                num[item.id] = item.count
            })
            return num
        },
        goodsSwitch (state){
            var states = {};
            state.car.forEach(item => {
                states[item.id] = item.selected
            })
            return states
        },
        getPrice(state){
            var p = {
                amount:0,
                price:0
            };
            state.car.forEach(item => {
                if(item.selected == true){
                    p.amount+=parseInt(item.count);
                    p.price+=parseInt(item.count)*parseInt(item.price)
                }
            })
            return p
        }
    }

})

//定义filter格式化时间
Vue.filter('timeData',function (datastr,pattern="YYYY-MM-DD HH:mm:ss"){
    moment(datastr).format(pattern)
})


//创建VM实例
var vm = new Vue({
    el: '#app',
    data: {
        msg: '我是测试'
    },
    render:function (createElements){
        return createElements(app)
    },
    router,
    store
    
})